<template>
  <section>
    <!--高级工具条开始-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.username" placeholder="员工名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="loadListData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--高级查询结束-->

    <!--列表开始-->
    <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="120" >
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="120" >
      </el-table-column>
      <el-table-column prop="salt" label="盐" width="120" >
      </el-table-column>
      <el-table-column prop="password" label="密码" width="120" >
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="120" >
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" :formatter="formatSex" >
      </el-table-column>
      <el-table-column prop="department_id" label="部门编号" width="120" >
      </el-table-column>
      <el-table-column prop="logininfo_id" label="登录信息" min-width="180" >
      </el-table-column>
      <el-table-column prop="shop_id" label="店号" min-width="180" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表结束-->

    <!--新增界面开始-->
    <!--
      label：要展示给用户看的数据
    -->
    <el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="editForm">
        <el-form-item label="员工名称" prop="username">
          <el-input v-model="addForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="addForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门编号">
          <el-input v-model="addForm.department_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店号">
          <el-input v-model="addForm.shop_id" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" >
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--新增界面结束-->

    <!--工具条开始-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes,prev, pager, next"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     :page-sizes="[5, 10, 20, 30]"
                     :page-size="pageSize"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>
    <!--工具条结束-->

  </section>
</template>


<script>

import {addUser, batchRemoveUser, removeUser} from "../../api/api";
import util from "../../common/js/util";

export default {
  data() {
    return{

      filters: {//高级查询绑定的事件
        username: ''
      },
      listLoading: true,//有加载的效果，true为开启
      listData:[],//用于列表展示
      sels: [],//列表选中列,多选选中的id数组
      total:0,
      page:1,//默认是第一页
      pageSize:5,//默认展示十条

      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      //新增界面需要填写的数据
      addForm: {

      },
      title:'',
      //提示用户填写信息
      addFormRules: {
      username: [
        { required: true, message: '请输入名称', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' }
      ],
      phone: [
        { required: true, message: '请输入电话', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ],

    },


    }
  },

  methods:{
    //当点击每页展示条数时
    handleSizeChange(val){
      this.pageSize = val
      this.loadListData()
    },

    //点击触发事件，把当前页数据给page
    handleCurrentChange(val) {
      this.page = val;
      this.loadListData();
    },

    //新增
    addSubmit () {
      //valid是一个布尔类型的值，true弹出模态框
      this.$refs.editForm.validate((valid) => {//校验表单， 通过返回true
        console.debug(valid)
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;

            this.$http.put("/employee",this.addForm).then(res =>{
              //关闭模态框
              this.addLthoading = false;
              let {success,message} = res.data
              if (success){
                //NProgress.done();
                this.$message({
                  message: '新增成功',
                  type: 'success'
                });
              }else {
                this.$message.error('操作失败');
              }
              this.addLoading = false;
              this.addFormVisible=false;
              //刷新数据
              this.loadListData()
            })
          });
        }
      });
    },

    //批量删除
    batchRemove () {
      //使用.toString()此时请求405，格式不匹配
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        //发送请求
        this.$http.patch("/employee",ids).then(res =>{
          let {success,message} = res.data
          if (success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error('错了哦，这是一条错误消息');
          }
          //刷新数据
          this.loadListData()
        })
      }).catch(() => {
        this.$message('取消删除成功');
      });
    },

    //普通删除
    handleDel (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        //开启加载数据动画
        this.listLoading = true;
        //发送请求
        this.$http.delete("/employee"+"/"+row.id).then(res =>{
        //通过解构的方式拿到后端传递回来的数据
          let {success,message} = res.data
          if (success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error('错了哦，这是一条错误消息');
          }
          //刷新数据
          this.loadListData();
        })

      }).catch(() => {
        this.$message('取消删除成功');
      });
    },

    //打开新增新增模态框
    handleAdd: function () {
      this.title="新增"
      this.addFormVisible = true;
      this.addForm = {
        username:'',
        email: '',
        phone:'',
        password:'',
        age:'',
        state: 0,
        department_id: '',
        shop_id: '',
      };
    },

    //状态显示转换
    formatSex (row, column) {
      return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
    },

    //修改
    handleEdit (index, row) {
      //回显数据
      this.addForm = Object.assign({}, row);
      //弹出模态框
      this.addFormVisible=true
      //给title定义
      this.title="修改"

    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    //发送请求的方法
    loadListData(){
      let param ={
        //把当前页码跟每页要展示的条数传递到后端
        currentPage:this.page,
        pageSize:this.pageSize,
        keywords:this.filters.username //高级查询绑定的事件
      }

      //发送axios请求
      this.$http.post("/employee",param).then(res =>{
        console.debug(res)
        this.listData=res.data.data
        this.total=res.data.totals
        this.listLoading=false
      }).catch(res =>{
      })
    },
  },

  mounted() {
    //刷新数据
    this.loadListData();

  }
}

</script>

<style scoped>

</style>